﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CARMSBusiness.InterFace;
using SD.LLBLGen.Pro.ORMSupportClasses;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMSBusiness
{
    public class AdminBusiness
    {


        #region  data dictionary
        public static DictionaryEntity GetDictionary(int itemID)
        {
            var item = new DictionaryEntity(itemID);
            if(!item.IsNew)
            {
                return item;
            }
            return null;
        }

        public static string GetDataName(Guid? dataValue)
        {
            if (dataValue.HasValue)
            {
                return GetDataName(dataValue.Value);
            }
            return string.Empty;
        }

        public static string GetDataName(int dataValue)
        {
            var data = new DictionaryEntity(dataValue);
            if (data.IsNew)
            {
                return string.Empty;
            }
            else
            {
                return data.Name;
            }
        }
        //save new
        public static bool SaveDictionary(DictionaryEntity entity)
        {
            if (entity.IsNew)
            {
                var collection = new DictionaryCollection();
                collection.GetMulti(DictionaryFields.CategoryId == entity.CategoryId, 0,
                                    new SortExpression { DictionaryFields.SortIndex | SortOperator.Descending });
                if (collection.Count > 0)
                {
                    entity.SortIndex = collection[0].SortIndex + 1;
                }
                else
                {
                    entity.SortIndex = "1";
                }
            }
            return entity.Save();
        }

        public static bool DeleteDictionary(int keyID)
        {
            var entity = new DictionaryEntity(keyID);
            if (!entity.IsNew)
            {
                return entity.Delete();
            }
            return false;
        }
        public static bool SaveDictionaryCategory(DictionaryCategoryEntity entity)
        {
            return entity.Save();
        }

        #endregion

        //public static bool DeletePublicItem(Guid itemID)
        //{
        //    var publicItem = new PublicItemEntity(itemID);
        //    if (!publicItem.IsNew)
        //    {
        //        return publicItem.Delete();
        //    }
        //    return false;
        //}

        #region role function


        public static bool SaveActionToRole(int RoleID, List<string> listFunctionID)
        {
            var collection = new RoleActionCollection();
            collection.DeleteMulti(RoleActionFields.RoleId == RoleID); 
            foreach (var item in listFunctionID)
            {
                var entity = new RoleActionEntity();
                entity.RoleId = RoleID;
                entity.ActionId = item;
                entity.Save();
            }
            return true; 
        }
        //public static bool CreatePublicType(PublicTypeEntity entity)
        //{
        //    return entity.Save();

        //}
        //public static bool CreatePublicItem(PublicItemEntity entity)
        //{
        //    return entity.Save();

        //}
        public static bool CreateRole(RoleEntity entity)
        {
            return entity.Save();

        }

        public static ActionEntity GetActionByID(string functionID)
        {
            var function = new ActionEntity(functionID);
            if (!function.IsNew)
            {
                return function;
            }
            return null;
        }


        public static ActionEntity GetActionParentByID(string functionID)
        {
            var function = new ActionEntity(functionID);
            if (!function.IsNew)
            {
                var parent = new ActionEntity(function.ParentId);
                if (!parent.IsNew)
                {
                    return parent;
                }
            }
            return null;
        }
        #endregion

        public static List<ActionEntity> GetActionList()
        {
            var functionCollection = new ActionCollection();
            functionCollection.GetMulti(null);
            var list = new List<ActionEntity>();
            foreach (var item in functionCollection)
            {
                list.Add(item);
            }
            return list;
        }


        //public static RecordCollection GetRecordList(PredicateExpression Condition, SortExpression sort, int pageSize, int pageIndex)
        //{
        //    var collection = new RecordCollection();
        //    collection.GetMulti(Condition, 0, sort, null, pageIndex, pageSize);
        //    return collection;
        //}


        public static string GetUserMailListByLoginNames(string strLoginNames)
        {
            var listLoginNames = strLoginNames.SeperateString(ApplicationConfiguration.StringSeperator);

            var fieldLoginName = new FieldCompareRangePredicate(UserFields.LoginName, false, listLoginNames);

            var collection = new UserCollection();
            collection.GetMulti(fieldLoginName);

            var mailList = from item in collection where item.Email.Trim().Length>0 select item.Email;

            return mailList.SeperateToString(ApplicationConfiguration.StringSeperator);
            
        }
    }
}
